/**
 * @author 蔡明宗
 * @date 2012-10-31 10:11:15
 */

package com.gossip.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gossip.data.jdbc.TipData;
import com.gossip.model.Tip;

/**
 * @author Administrator
 * 
 */
public class Index extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7261874817353549920L;
	List<Tip> tips;
	String script;
	String fun;
	String fun2;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		RequestDispatcher dispatcher;

		tips = TipData.queryTips();
		generateScript();

		req.setAttribute("data", tips);
		req.setAttribute("script", script);
		req.setAttribute("fun", fun);
		req.setAttribute("fun2", fun2);
		dispatcher = this.getServletContext().getNamedDispatcher("home");
		dispatcher.forward(req, resp);
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	private void generateScript() {
		StringBuffer s = new StringBuffer();
		StringBuffer s2 = new StringBuffer();
		StringBuffer s3 = new StringBuffer();
		StringBuffer s4 = new StringBuffer();
		StringBuffer temp = new StringBuffer();

		s.append("Drag.initDivs(new Array(");
		s2.append("fadeOutAll('fadeOut',1000,new Array(");
		s3.append("fadeOutAll('fadeIn',1000,new Array(");
		s4.append("generateRandom(new Array(");
		Tip tip = null;
		for (int i = 0; i < tips.size() - 1; i++) {
			tip = tips.get(i);
			temp.append("'").append(tip.getId()).append("',");
		}
		tip = tips.get(tips.size() - 1);
		temp.append("'").append(tip.getId()).append("'));");

		script = s.append(temp).toString() + s4.append(temp).toString();
		fun = s2.append(temp).toString();
		fun2 = s3.append(temp).toString();

	}
}
